/*
 * 文件名: YearMonthDayAmPmDialog.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [该类的简要描述]
 * 创建人: EX-XUJIAO001
 * 创建时间: 2011-12-28
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package app.widget.calendar.dialog;

import java.util.Calendar;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import app.log.APPLog;
import app.utils.CommonUtils;
import app.widget.BaseDialog;
import app.widget.calendar.widget.OnWheelChangedListener;
import app.widget.calendar.widget.OnWheelScrollListener;
import app.widget.calendar.widget.WheelView;
import app.widget.calendar.widget.adapter.DateArrayAdapter;
import app.widget.calendar.widget.adapter.DateNumericAdapter;

import com.longevityassistant.activity.R;

/**
 * @author EX-XUJIAO001
 * @date 2011-12-28
 * @version [Android PABank C01, @2011-12-28]
 * @description 年月日历 dialog 继承BaseDialog
 */
public class YearMonthDialog extends BaseDialog implements
		OnWheelChangedListener, OnWheelScrollListener {
	/**
	 * 结果
	 */
	private String result;
	/**
	 * 当前年
	 */
	private int curYear;
	/**
	 * 当前月
	 */
	private int curMonth;
	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 设置最小日历
	 */
	private Calendar minTime;
	/**
	 * 设置最大日历
	 */
	private Calendar maxTime;
	/**
	 * 最小年
	 */
	private int minYear;
	/**
	 * 最大年
	 */
	private int maxYear;
	/**
	 * 最小月
	 */
	private int minMonth;
	/**
	 * 最大月
	 */
	private int maxMonth;
	/**
	 * 月Wheelview
	 */
	private WheelView month;
	/**
	 * 年Wheeleview
	 */
	private WheelView year;
	/**
	 * 返回view
	 */
	private View contentView;

	/**
	 * 是否滚动
	 */
	private boolean isScroll;

	/**
	 * 
	 * [构造简要说明]
	 * 
	 * @param context
	 *            上下文
	 * @param title
	 *            标题
	 * @param minTime
	 *            最小日历
	 * @param maxTime
	 *            最大日历
	 */
	public YearMonthDialog(Context context, String title, Calendar minTime,
			Calendar maxTime) {

		super(context);
		this.context = context;
		setTitle(title);
		this.minTime = minTime;
		this.maxTime = maxTime;
		maxYear = maxTime.get(Calendar.YEAR);
		maxMonth = maxTime.get(Calendar.MONTH);

		initDate();
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 是否能反弹
	 * @return boolean
	 */
	public boolean isScroll() {
		return isScroll;
	}
	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * @param isScroll 反弹标志
	 */
	public void setScroll(boolean isScroll) {
		this.isScroll = isScroll;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化
	 */
	private void initDate() {

		initView();
		CalendarDialogCommon.addChangeListener(this, year, month);
		setBtn1ClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				onDialogScrollEndListener.onDialogScrollEnd(result);
				YearMonthDialog.this.dismiss();
			}
		});

		setBtn2ClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				YearMonthDialog.this.dismiss();
			}
		});

		Calendar calendar = CommonUtils.getCurrentTimeByCalendar();

		// month
		curMonth = calendar.get(Calendar.MONTH);
		curMonth = curMonth + 1;

		month.setViewAdapter(new DateArrayAdapter(context,
				CalendarDialogCommon.MONTHS, curMonth));
		month.setCurrentItem(curMonth);

		// year
		curYear = calendar.get(Calendar.YEAR);

		if (minTime != null) {
			minYear = minTime.get(Calendar.YEAR);
			minMonth = minTime.get(Calendar.MONTH) + 1;
		} else {
			minYear = curYear;
		}

		if (maxTime != null) {
			maxYear = maxTime.get(Calendar.YEAR);
			maxMonth = maxTime.get(Calendar.MONTH) + 1;
			curYear = maxYear;
		} else {
			maxYear = curYear + 100;
		}
		APPLog.i("zbkc", "minYear:"+minYear+"maxYear:"+maxYear+"minMonth:"+minMonth+"maxMonth:"+maxMonth);
		year.setViewAdapter(new DateNumericAdapter(context, minYear, maxYear,
				curYear - minYear, "年"));
		year.setCurrentItem(curYear - minYear);

		result = curYear + "-"
				+ CalendarDialogCommon.convertZeroString(curMonth);
		APPLog.i("zbkc", result);
		
		year.addScrollingListener(this);
		month.addScrollingListener(this);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 初始化view
	 */
	private void initView() {

		contentView = LayoutInflater.from(context).inflate(
				R.layout.app_widget_year_month_layout, null);
		month = (WheelView) contentView.findViewById(R.id.month);
		year = (WheelView) contentView.findViewById(R.id.year);
	}

	/**
	 * 监听器
	 */
	private OnDialogScrollEndListener onDialogScrollEndListener;

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] setter
	 * 
	 * @param onDialogScrollEndListener
	 *            滑动结束监听器
	 */
	public void setOnDialogScrollEndListener(
			OnDialogScrollEndListener onDialogScrollEndListener) {
		this.onDialogScrollEndListener = onDialogScrollEndListener;
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] getter
	 * 
	 * @return OnDialogScrollEndListener
	 */
	public OnDialogScrollEndListener getOnDialogScrollEndListener() {
		return onDialogScrollEndListener;
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		switch (wheel.getId()) {
		case R.id.year:
			curYear = minYear + newValue;
			break;
		case R.id.month:
			curMonth = newValue + 1;
			break;
		default:
			break;
		}

		/*
		 * Log.i("zbkc", oldValue+"|"+newValue); Log.i("zbkc",
		 * "zbkc:"+wheel.getCurrentItem());
		 */

		result = curYear + "-"
				+ CalendarDialogCommon.convertZeroString(curMonth);
		APPLog.i("zbkc", result);
	}

	/**
	 * 
	 * [一句话功能简述]<BR>
	 * [功能详细描述] 选择年月
	 * 
	 * @param y
	 *            年
	 * @param m
	 *            月
	 * 
	 */
	public void select(int y, int m) {

		APPLog.i("zbkc", "year:" + y + "month:" + m);
		month.setCurrentItem(m - 1);
		year.setCurrentItem(y - minYear);
		month.invalidate();
		year.invalidate();
	}

	@Override
	public View createContentView() {
		// TODO Auto-generated method stub
		return contentView;
	}

	@Override
	public void onScrollingStarted(WheelView wheel) {
		setBtn1Enable(false);
		setBtn2Enable(false);
	}

	@Override
	public void onScrollingFinished(WheelView wheel) {

		if (isScroll) {
			if (curYear == minYear && curMonth < minMonth) {
				month.scroll(minMonth - curMonth, 0);
			} else if (curYear == maxYear && curMonth > maxMonth) {
				month.scroll(maxMonth - curMonth, 0);
			}
		}
		setBtn1Enable(true);
		setBtn2Enable(true);
	}

}
